package com.aznest.service.impl;

import lombok.Data;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.ConcurrentHashMap;

@Service
@Data
public class BloomFilterService {
    @Autowired
    RedissonClient redissonClient;
    private ConcurrentHashMap<String, RBloomFilter<Integer>> bloomFilters = new ConcurrentHashMap<>();
    public RBloomFilter<Integer> initBloomFilter(String cacheName, int expectedInsertions, double fpp) {
        RBloomFilter<Integer> cachePenetrationBloomFilter = redissonClient.getBloomFilter(cacheName);
        cachePenetrationBloomFilter.tryInit(expectedInsertions, fpp);
        bloomFilters.put(cacheName, cachePenetrationBloomFilter);
        return bloomFilters.get(cacheName);
    }

    public void deleteBloomFilter(String cacheName) {
        bloomFilters.remove(cacheName);
    }
}
